package com.alibaba.algo.domain.user;

import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.dao.user.SysUserEmployeeMapper;
import com.alibaba.algo.entity.user.SysEmployee;
import com.alibaba.algo.entity.user.SysUser;
import com.alibaba.algo.entity.user.SysUserEmployee;
import com.alibaba.algo.exception.SysUserEmployeeErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 类SysUserEmployee.java的实现描述：用户员工表Domain
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Component
public class SysUserEmployeeDomain extends BaseDomain<SysUserEmployee> {

    @Resource
    private SysUserEmployeeMapper sysUserEmployeeMapper;
    @Autowired
    private SysUserDomain sysUserDomain;
    @Autowired
    private SysEmployeeDomain sysEmployeeDomain;

    /**
     * 根据用户标识查询员工详情  有效数据
     *
     * @param userId 用户标识
     * @return 员工详情
     */
    public SysEmployee selectEmployeeByUserId(Long userId) {
        return sysUserEmployeeMapper.selectEmployeeByUserId(userId);
    }

    /**
     * 根据用户标识查询员工详情 全部数据
     *
     * @param userId 用户标识
     * @return 员工详情
     */
    public SysEmployee selectEmployeeByUserIdDefaultAll(Long userId, String isDeleted) {
        return sysUserEmployeeMapper.selectEmployeeByUserIdDefaultAll(userId, isDeleted);
    }

    public Integer deleteByUserId(Long userId) {
        return sysUserEmployeeMapper.deleteByUserId(userId);
    }

    /**
     * 根据employeeId查询userId
     * @param employeeId
     * @return
     */
    public Long selectUserIdByEmployeeId(Long employeeId){
        return sysUserEmployeeMapper.selectUserIdByEmployeeId(employeeId);
    }

    public void updateSysUserEmployeeByUserId(SysUserEmployee sysUserEmployee){
        sysUserEmployeeMapper.updateByUserId(sysUserEmployee);
    }

    public void updateSysUserEmployeeByUserIdLastModified(SysUserEmployee sysUserEmployee, LocalDateTime lastModifyTime){
        sysUserEmployeeMapper.updateByUserIdLastModified(sysUserEmployee,lastModifyTime);
    }

    public Integer updateByIdWithIsDeleted(SysUserEmployee sysUserEmployee){
        return sysUserEmployeeMapper.updateByIdWithIsDeleted(sysUserEmployee);
    }

    /**
     * 校验并添加用户-员工关系信息
     * @param userId
     * @param employeeId
     */
    public void checkAndAddRelation(Long userId, Long employeeId) {
        Long userIdByEmployeeId = this.selectUserIdByEmployeeId(employeeId);
        if(!ObjectUtils.isEmpty(userIdByEmployeeId)){
            // 判断查询出的userId和传参中的userId是否相等，不等则说明已关联其他用户，否则不用处理
            if(!userId.equals(userIdByEmployeeId)){
                throw new BaseException(SysUserEmployeeErrorCode.USER_EMLOYEE_REPEAT.getCode(),SysUserEmployeeErrorCode.USER_EMLOYEE_REPEAT.getMessage());
            }
        }else{
            // 历史遗留问题需要更新sys_user_employee和sys_user两张表
            // 更新sys_user_employee表
            SysUserEmployee sysUserEmployee = new SysUserEmployee();
            sysUserEmployee.setUserId(userId);
            sysUserEmployee.setEmployeeId(employeeId);
            SysEmployee sysEmployee = this.selectEmployeeByUserId(userId);
            if(ObjectUtils.isEmpty(sysEmployee)){
                // 新增员工关联信息
                this.insert(sysUserEmployee);
            }else{
                // 更新员工关联信息
                this.updateSysUserEmployeeByUserId(sysUserEmployee);
            }
            // 更新sys_user表
            SysEmployee employee = sysEmployeeDomain.getById(employeeId);
            SysUser sysUser = new SysUser();
            sysUser.setId(userId);
            sysUser.setEmployeeCode(employee.getEmployeeCode());
            sysUserDomain.updateById(sysUser);
        }
    }

    public Integer countByEmployeeIdAndUserId(SysUserEmployee obj){
        return sysUserEmployeeMapper.countByEmployeeIdAndUserId(obj);
    }

    public List<SysUserEmployee> listByEmployeeId(SysUserEmployee sysUserEmployee){
        return sysUserEmployeeMapper.listByEmployeeId(sysUserEmployee);
    }

    public List<SysUserEmployee> listByEmployeeIdLastModified(SysUserEmployee sysUserEmployee,LocalDateTime lastModifyTime){
        return sysUserEmployeeMapper.listByEmployeeIdLastModified(sysUserEmployee,lastModifyTime);
    }

    public List<SysUserEmployee> listByParaLastModified(SysUserEmployee sysUserEmployee,LocalDateTime lastModifyTime,String sort){
        return sysUserEmployeeMapper.listByParaLastModified(sysUserEmployee,lastModifyTime,sort);
    }

}
